#include "Collision.h"
#include <stdio.h>

int collision (const float posX_h, const float posY_h, const float tailleX_h, 
		const float tailleY_h, 
		const float posX_e, const float posY_e, const float tailleX_e, 
		const float tailleY_e)
{
	if ((posY_h >= posY_e + tailleY_e) || (posY_h + tailleY_h <= posY_e)
	 || (posX_h >= posX_e + tailleX_e) || (posX_h + tailleX_h <= posX_e))
		return 1; /* Pas de collision */
	else
		return 0; /* collision ! BOUM ! */
}


int collisionHerosEnnemi (const Heros* pheros, const Ennemi* pennemi)
{
	return collision (pheros->posX_heros, pheros->posY_heros, 
			pheros->tailleX_heros, pheros->tailleY_heros, 
			pennemi->posX_ennemi, pennemi->posY_ennemi, 
			pennemi->tailleX_ennemi, pennemi->tailleY_ennemi);
}

int collisionHerosBoss (const Heros* pheros, const Boss* pboss)
{
	return collision (pheros->posX_heros, pheros->posY_heros, 
			pheros->tailleX_heros, pheros->tailleY_heros, 
			pboss->posX_boss, pboss->posY_boss, 
			pboss->tailleX_boss, pboss->tailleY_boss);
}

int collisionHerosBonus (const Heros* pheros, const Bonus* pbonus)
{
	return collision (pheros->posX_heros, pheros->posY_heros, 
			pheros->tailleX_heros, pheros->tailleY_heros, 
			pbonus->posX_bonus, pbonus->posY_bonus, 
			33.0/8.0, 30.0/4.0);
}

int collisionTirHeros (const Tir* ptir, const Heros* pheros)
{
	if (ptir->tir_ami == 1)
	{
		return collision (pheros->posX_heros, pheros->posY_heros, 
				pheros->tailleX_heros, pheros->tailleY_heros, 
				ptir->posX_tir, ptir->posY_tir, 
				ptir->tailleX_tir, ptir->tailleY_tir);
	}
	else
		return 1;
}

int collisionTirEnnemi (const Tir* ptir, const Ennemi* pennemi)
{
	if (ptir->tir_ami != 1)
	{
		return collision (pennemi->posX_ennemi, pennemi->posY_ennemi, 
				pennemi->tailleX_ennemi, pennemi->tailleY_ennemi, 
				ptir->posX_tir, ptir->posY_tir, 
				ptir->tailleX_tir, ptir->tailleY_tir);
	}
	else
		return 1;
}

int collisionTirEnnemiS1 (const Tir* ptir, const Ennemi* pennemi)
{
	if (ptir->tir_ami == 2)
	{
		return collision (pennemi->posX_ennemi, pennemi->posY_ennemi,
				pennemi->tailleX_ennemi, pennemi->tailleY_ennemi,
				ptir->posX_tir-5, ptir->posY_tir-5,
				ptir->tailleX_tir+5, ptir->tailleY_tir+5);
	}
	else
		return 1;
}

int collisionTirBoss (const Tir* ptir, const Boss* pboss)
{
	if (ptir->tir_ami == 1)
		return 1;
	else
	{
		return collision (pboss->posX_boss, pboss->posY_boss, 
				pboss->tailleX_boss, pboss->tailleY_boss, 
				ptir->posX_tir, ptir->posY_tir, 
				ptir->tailleX_tir, ptir->tailleY_tir);
	}
}

/** @brief Cette fonction teste toutes les fonctions du module*/
void testRegressionCollision ()
{
	Point* ppt1;
	ppt1= creerPoint(0, 0);
	Point* ppt2 ;
	ppt2 = creerPoint(5, 10);
	double vit1 = 10.5;
	
	int PV = 10;
	int posX = 0;
	int posY = 0;
	int tailleX = 3;
	int tailleY = 4;
	
	Trajectoire* ptraj = creerTrajectoire (ppt1, ppt2, vit1);
	
	ArmePrincipale* parm_prin = creerArmePrincipale ();
	ArmeSecondaire* parm_sec1 = creerArmeSecondaire (0);
	ArmeSecondaire* parm_sec2 = creerArmeSecondaire (1);
	
	Heros* pheros;

	pheros = creerHeros (parm_prin, parm_sec1, parm_sec2, PV, posX, posY,
		 tailleX, tailleY);
	
	Boss* pboss;
	
	pboss = creerBoss (1, posX, posY, ptraj, 0);
	
	Ennemi* pennemi;
	
	pennemi = creerEnnemi (0, posX+5, posY+2, ptraj);
	
	Tir* ptir;
	
	ptir = creerTir (1,1,1,1, ptraj, 1);
	
	Bonus* pbonus;
	
	pbonus = creerBonus (pennemi);
	
	int res;
	
	printf ("0 = Collision, 1 = Pas Collision \n\n");
	
	res = collisionHerosEnnemi (pheros, pennemi);
	printf("collisionHerosEnnemi -> %d\n\n", res);

	res = collisionHerosBoss (pheros, pboss);
	printf("collisionHerosBoss -> %d\n\n", res);

	res = collisionHerosBonus (pheros, pbonus);
	printf("collisionHerosBonus -> %d\n\n", res);

	res = collisionTirHeros (ptir, pheros);
	printf("collisionTirHeros -> %d\n\n", res);

	res = collisionTirEnnemi (ptir, pennemi);
	printf("collisionTirEnnemi -> %d\n\n", res);

	res = collisionTirBoss (ptir, pboss);
	printf("collisionTirBoss -> %d\n\n", res);
	
}
